/*
 * VideoCapture.hh
 *
 *  Created on: 2014-4-9
 *      Author: root
 */
#ifndef VIDEOCAPTURE_HH_
#define VIDEOCAPTURE_HH_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <inttypes.h>
#include <linux/videodev2.h>


#define REQ_COUNT 4


///*
struct buffer {
	void* start;
	size_t length;//buffer's length is different from cap_image_size
};
typedef struct buffer FRAME_BUF;
//*/

class VideoCapture
{
public:
	int  Capture_Frame(FRAME_BUF *outBuf);
	void Capture_Device_init(const char* dev_name);
	void Capture_Device_release(void);
	static VideoCapture* createNew(int Width ,int Height);
	~VideoCapture();
protected:
	VideoCapture(int Width ,int Height);

private:
	//私有变量
	FRAME_BUF * buffers;
	unsigned int n_buffers;
	unsigned int cap_image_size;//to keep the real image size!!
	uint8_t *frame_420;
	int img_width;
	int img_height;
	int fd ;
	//函数声明部分
	void errno_exit(const char* s);
	int xioctl(int fd, int request, void* arg);
	void open_device(const char* dev_name);
	void init_device (const char* dev_name);
	void init_mmap(const char* dev_name);
	void start_capturing(void);
	int read_frame(FRAME_BUF *outbuf);
	void stop_capturing(void);
	void uninit_device(void);
	void close_device(void);
	void change_422_to_420(const uint8_t* frame_422,int width, int height);

};

#endif /* VIDEOCAPTURE_HH_ */
